<?php
	session_start();
	$user = isset($_SESSION['user'])?$_SESSION['user']:false;
	require_once $_SERVER['DOCUMENT_ROOT'].'/myblog1/lib/Db.php';
	$db = new Db();

	$cid = isset($_GET['cid'])?$_GET['cid']:0;
	$path = '/myblog1/index.php';
	// 查询条件
	$where=[];
	if($cid){
		$where['cid'] = $cid;
		$path.='?cid='.$cid;
	}

	$page = max(1,isset($_GET['page'])?$_GET['page']:1);
	$pageSize = 2;
	
	$data = $db->table('article')->where($where)->field('id,cid,title,pv,add_time')->pages($page,$pageSize,$path);



	$cids = [];
	foreach ($data['data'] as $article) {
		if(!in_array($article['cid'],$cids)){
			$cids[] = $article['cid'];
		}
	}
	$cates = $db->table('cates')->where('id in('.implode(',', $cids).')')->cates('id');

	foreach ($data['data'] as $key => $value) {
		$data['data'][$key]['cate_title'] = $cates[$value['cid']]['title'];
	}
?>
<!DOCTYPE html>
<html>
<head>
	<title>我的博客</title>

	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="./static/css/site.css">
	<link rel="stylesheet" type="text/css" href="./static/plugins/bootstrap/css/bootstrap.min.css">
	<script type="text/javascript" src="./static/js/jquery.min.js"></script>
	<script type="text/javascript" src="./static/plugins/bootstrap/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="./static/js/util.js"></script>
</head>
<body>
	<!--header-->
	<div class="header">
		<div class="container">
			<span class="title">刘永鹏的博客</span>
			<div class="search">
				<div class="input-group">
				  <input type="text" class="form-control" placeholder="输入博客名称搜索">
				  <span class="input-group-btn">
			        <button class="btn btn-default" type="button">搜索</button>
			      </span>
				</div>
			</div>
			<div class="login-reg">
				<?php if($user){?>
				<span style="color:#fff;"><?php echo $user['username']?></span><a href="javascript:;" onclick="logout()">退出</a>
				<?php }else{?>
				<button class="btn btn-success" onclick="login()">登录</button>
				<?php }?>

				<button class="btn btn-warning" onclick="add_article()">发布博客</button>
			</div>
		</div>
	</div>

	<!--main-->
	<div class="container main">
		<div class="col-lg-3 content-left">
			<p class="cate">博客分类</p>
			<div class="cate-list">
				<p><a href="./index.php?cid=1">编程语言</a></p>
				<p><a href="./index.php?cid=2">软件设计</a></p>
				<p><a href="./index.php?cid=3">Web前端</a></p>
				<p><a href="">企业信息化</a></p>
				<p><a href="">安卓开发</a></p>
				<p><a href="">IOS开发</a></p>
				<p><a href="">软件工程</a></p>
				<p><a href="">数据库技术</a></p>
				<p><a href="">操作系统</a></p>
				<p><a href="">其他分类</a></p>
			</div>
		</div>
		<div class="col-lg-9">
			<div class="nav">
				<a href="">热门</a>
				<a href="./index.php" class="active">最新</a>
			</div>

			<div class="content-list">
				<?php foreach($data['data'] as $item){?>
				<div class="content-item">
					<img src="./static/image/avatar.png">
					<div class="title">
						<p><a href="./detail.php?id=<?php echo $item['id']?>"><?php echo $item['title']?></a></p>
						<div><span>[<?php echo $item['cate_title']?>]<?php echo $item['pv']?>次浏览</span><span><?php echo date('Y-m-d H:i:s',$item['add_time'])?></span></div>
					</div>
				</div>
				<?php }?>

			</div>
			
			<div class="pages">
			    <nav aria-label="...">
			     <?php echo $data['pages']?>
			   </nav>
			</div>
		</div>
	</div>



</body>
</html>
<script type="text/javascript">
	//$('#myModal').modal({backdrop:'static'});
	//$('#myModal').modal('show');

	//UI.alert({msg:'请输入用户名',title:'系统提示'});
	//UI.open({url:'http://www.baidu.com'});

	// 登录
	function login(){
		UI.open({title:'登录',url:'./login.php',width:480,height:350});
	}

	// 退出登录
	function logout(){
		$.get('./service/logout.php',{},function(res){
			if(res.code>0){
				UI.alert({msg:res.msg,icon:'error'});
			}else{
				UI.alert({msg:res.msg,icon:'ok'});
				setTimeout(function(){window.location.reload();},1000);
			}
		},'json');
	}

	// 发布博客
	function add_article(){
		UI.open({
			title:'发表博客',
			url:'add_article.php',
			width:700,
			height:650
		});
	}
</script>